package com.intel.factorytest.activity;

import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.os.Handler;
import android.os.Message;
import com.intel.factorytest.R;
import com.intel.factorytest.util.Util;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;

public class VibratorTest extends BaseActivity implements OnClickListener{
    ContentResolver mResolver;
    TextView vibrateText;
    private int b_value;
    private static final String TEST_RESULT = "test_result";
    private static final int VIB_TEST_OPEN = 1;
    private static final int VIB_TEST_CLOSE = 0;
    private static final long VIB_TIME_DELAY = 300;
    private static final String TAG = "VibratorTest";
    private int vib_times = 0;
    private Button mSucBtn;
    private Button mFailBtn;
    private Button mRepeatBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        Util.wakeUpAndKeepScreenOn(this);
        setContentView(R.layout.vibrator_layout);        
        AudioManager audioManager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setMode(AudioManager.RINGER_MODE_VIBRATE);
//        audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);
        initView();
        mSucBtn = (Button) findViewById(R.id.success_button);
        mFailBtn = (Button) findViewById(R.id.fail_button);
        mRepeatBtn = (Button) findViewById(R.id.repeat_button);
        // mSucBtn.setEnabled(false);
        // mFailBtn.setEnabled(false);
        // mRepeatBtn.setEnabled(false);
    } 
    
    @Override
    protected void onDestroy(){
        myHandler.removeMessages(VIB_TEST_OPEN);
        myHandler.removeMessages(VIB_TEST_CLOSE);
        vibrating(false, null);
        super.onDestroy();
    }
    
    @Override
    protected void onResume(){
        // vibrateText.setText(R.string.SERVICEMENU_VIBRATE_TXT);
        // Message msg = new Message();
        // msg.what = VIB_TEST_OPEN;
        // myHandler.sendMessageDelayed(msg, VIB_TIME_DELAY);
        // vibrating(true);
        super.onResume();
    }

    protected void onPause(){
        //vibrating(false);
        super.onPause();
    } 
    
    protected void initView() {
        super.initView();
        vibrateText = (TextView) findViewById(R.id.vibratorTextView);

        Button btn1 = (Button) findViewById(R.id.btn_1);
        Button btn2 = (Button) findViewById(R.id.btn_2);
        Button btn3 = (Button) findViewById(R.id.btn_3);
        Button btn4 = (Button) findViewById(R.id.btn_4);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
    }

    public void onClick(View view) {
        long[] times;// = new long[4];
        switch (view.getId()) {
            case R.id.btn_1:
//                times[0] = 1200;
//                times[1] = 2800;
                times = new long[] {1200, 2800, 1200, 2800};
                break;
            case R.id.btn_2:
//                times[0] = 1200;
//                times[1] = 5000;
                times = new long[] {1200, 5000, 1200, 5000};
                break;
            case R.id.btn_3:
//                times[0] = 600;
//                times[1] = 600;
                times = new long[] {600, 600, 600, 600};
                break;
            case R.id.btn_4:
//                times[0] = 2000;
//                times[1] = 2000;
                times = new long[] {2000, 2000, 2000, 2000};
                break;
            default:
//                times[0] = 1200;
//                times[1] = 2800;
                times = new long[] {1200, 2800, 1200, 2800};
                break;
        }
        vibrating(true, times);
    }


    /*
    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            long[] times = new long[2];
            switch (view.getId()) {
                case R.id.btn_1:
                    times[0] = 1200;
                    times[1] = 2800;
                    break;
                case R.id.btn_2:
                    times[0] = 1200;
                    times[1] = 5000;
                    break;
                case R.id.btn_3:
                    times[0] = 600;
                    times[1] = 600;
                    break;
                case R.id.btn_4:
                    times[0] = 2400;
                    times[1] = 1200;
                    break;
                default:
                    times[0] = 1200;
                    times[1] = 2800;
                    break;
            }
            vibrating(true, times);
        }
    };
    */
    private void vibrating(boolean enable, long[] vibrateTime) {
        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        Log.d(TAG, "vibrator = "+vibrator);
        if(vibrator == null) {
            vibrateText.setText(R.string.SERVICEMENU_NO_VIBRATE_TXT);
            return;
        }
        if (enable){
            if (vibrateTime == null || vibrateTime.length < 2) {
                vibrateTime = new long[] {1200, 2800, 1200, 2800};
//                vibrateTime[0] = 1200;
//                vibrateTime[1] = 2800;
            }
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                Log.d(TAG, "vibrating, android O API level");
                VibrationEffect vibe = VibrationEffect.createWaveform(vibrateTime, 0);
                vibrator.vibrate(vibe);
            } else {
                vibrator.vibrate(vibrateTime, 0);
            }
//            vibrator.vibrate(vibrateTime, 0);//asynchronize
            vibrateText.setText(R.string.SERVICEMENU_VIBRATE_TXT);
        } else {
            vibrator.cancel();
        }
    }

    @Override
    public void finish() {
        tc.setTestResult(is_pass);
        super.finish();
    }

    public boolean ac_ispass() {
        return is_pass;
    } 

    Handler myHandler = new Handler(){
           public void handleMessage(Message msg){
              mItemTestFinish = true;//add by wangsm
              switch(msg.what)
              {
                  case VIB_TEST_OPEN:
                     Log.d(TAG," wangsm VIB_TEST_OPEN");

                     vibrating(true, null);
                     mSucBtn.setEnabled(true);
                     mFailBtn.setEnabled(true);
                     mRepeatBtn.setEnabled(true);
                    // vib_times++;
                   /*  Message msgClose = new Message();
                     msgClose.what = VIB_TEST_CLOSE;
                     myHandler.sendMessageDelayed(msgClose, VIB_TIME_DELAY);*/
                  break;

                  case VIB_TEST_CLOSE:
                         Log.d(TAG," wangsm VIB_TEST_CLOSE");
                         vibrating(false, null);
                         Message msgOpen = new Message();
                         msgOpen.what = VIB_TEST_OPEN;
                         myHandler.sendMessageDelayed(msgOpen, VIB_TIME_DELAY);
                         /*if(vib_times < 3){
                             Message msgOpen = new Message();
                             msgOpen.what = VIB_TEST_OPEN;
                             myHandler.sendMessageDelayed(msgOpen, VIB_TIME_DELAY);
                         }else{
                             mSucBtn.setEnabled(true);
                             mFailBtn.setEnabled(true);
                             mRepeatBtn.setEnabled(true);
                         }*/

                  break;
               }
           }
         };

}
